Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix caching of dependencies during re-entrancy #249

Merged
merged 9 commits into from
Jul 22, 2024
Merged

Fix caching of dependencies during re-entrancy #249

merged 9 commits into from
Jul 22, 2024

Conversation

mbrandonw
Copy link
Member

This fixes a problem introduced in #235 that can cause dependencies to not be cached when first accessed in a re-entrant fashion. This can happen if while resolving a dependency one synchronously accesses another dependency:

struct OuterDependency: TestDependencyKey {
  static var testValue: OuterDependency {
    @Dependency(InnerDependency.self) var innerDependency
    _ = innerDependency
    return Self()
  }
}

The act of touching innerDependency synchronously while resolving OuterDependency causes re-entrancy into the cache of dependencies. And because #235 started protecting that cache with LockIsolated.withValue { … }, that causes the resolution of innerDependency to not be cached.

For now we will go back to protecting this state with a separate lock + mutable value, but in the future we may consider not allowing this kind of re-entrance into the cache. I think it's very subtle, and there are other ways of accomplishing this while avoiding re-entrancy.

@mbrandonw mbrandonw requested a review from stephencelis July 22, 2024 14:50
@mbrandonw mbrandonw merged commit d5798b5 into main Jul 22, 2024
3 checks passed
@mbrandonw mbrandonw deleted the fix-cache branch July 22, 2024 19:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants